/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package layouteditor.view;

import folder.helper.MenuLayoutElement;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import layoutitems.CurrentLayoutElements;
import layoutitems.ImageElement;
import layoutitems.LayoutElements;

/**
 *
 * @author ST
 */
public class InsertOnRightClickMenu implements ActionListener {
    
    private static JPopupMenu popUpMenu;
    private static JMenuItem menuItemInsert;
    private static JComponent frame;
    private static ArrayList<MenuLayoutElement> submenuArray;
    
    public InsertOnRightClickMenu(JComponent jFrame) {
        frame = jFrame;
        popUpMenu = new JPopupMenu();
        submenuArray = makeMenuItemsFromLayoutElements(this);
        menuItemInsert = new JMenu("Neues Layout Element einfügen");
        for (MenuLayoutElement mle : submenuArray) {
            menuItemInsert.add(mle.getMenuItem());
        }
        popUpMenu.add(menuItemInsert);
        
    }
    //##########################
    //      GETTER
    //##########################    
    public static ArrayList<MenuLayoutElement> getMenuLayoutElementConnectionArray(ActionListener al) {
        return makeMenuItemsFromLayoutElements(al);
    }
    //##########################
    //      Inner Methods
    //##########################

    public void showMenu(JComponent frameLayeredPane, int x, int y) {
        popUpMenu.show(frameLayeredPane, x, y);
    }
    
    private static ArrayList<MenuLayoutElement> makeMenuItemsFromLayoutElements(ActionListener al) {
        ArrayList<MenuLayoutElement> subArray = new ArrayList<MenuLayoutElement>();
        for (Iterator<ImageElement> it = LayoutElements.standardElements.iterator(); it.hasNext();) {
            ImageElement element = it.next();
            //System.out.println(element.getName());
            subArray.add(new MenuLayoutElement(element, al));
            //System.out.println(subArray.toString());
        }    
        return subArray;
    }

    //##########################
    //      ActionListener
    //##########################
    public void actionPerformed(ActionEvent e) {
        for (MenuLayoutElement mle : submenuArray) {
            if (e.getSource() == mle.getMenuItem()) {
                if (!mle.getImageElement().getBlocked()){
                    ImageElement insert = new ImageElement();
                    insert.clone(mle.getImageElement());
                    CurrentLayoutElements.allVisibleLayoutElements.add(insert);
                    frame.repaint();
                }
            }
        }
    }
    
 
}
